package seed.web.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import seed.domain.P_recommend;
import seed.domain.Seed;
import seed.service.SeedService;
import user.domain.User;

/**
 * Servlet implementation class IndexServlet
 */
public class IndexServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private SeedService seedService = new SeedService();   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public IndexServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("session_user");
		List<P_recommend> recommendList = null;
		List<Seed> recommendSeed = new ArrayList<Seed>();
		if(user != null)
		{
			recommendList = seedService.getPersonalRecommend(user.getUid());
			for(P_recommend p: recommendList){
				Seed s = seedService.getSeedBySid(p.getSid());
				recommendSeed.add(s);
			}
		}
		
		request.setAttribute("recommendSeed", recommendSeed);
		request.getRequestDispatcher("/index.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}

}
